#-*- coding:utf-8 -*-
from django.db import models
from django.core.urlresolvers import reverse
import datetime

class Link(models.Model):
    title = models.CharField(max_length = 256)
    url = models.URLField(verify_exists=False)
    insite_url = models.URLField(verify_exists=False,editable=True)
    added_date = models.DateField(auto_now_add=True)
    def __unicode__(self):
        return "link %s" % (self.title)

    def insite_link(self):
        return reverse('dj_outbound_stats.views.outbound',args=(self.id,))

    def save(self,force_insert=False,force_update=False):
        super(Link,self).save(force_insert,force_update)        
        self.insite_url = self.insite_link()
        super(Link,self).save(force_insert,force_update)

    def click_count(self,date=None):
        return self.click_set.count()


class Click(models.Model):
    date = models.DateField(auto_now_add=True)
    link = models.ForeignKey("Link")
    def __unicode__(self):
        return "link %s  (%s)click record" % (self.link.title , self.date)


